import { find } from "../../../api/shop"
import { list } from "../../../api/chef"
import { list as productList } from "../../../api/product"
const util = require('../../../utils/util');
const app = getApp();
// 注意传入商品id和shopId
Page({

  /**
   * 页面的初始数据
   */
  data: {
    APP_OSSURL_API: app.APP_OSSURL_API,
    imageList: new Array(4).fill(app.APP_OSSURL_API + "/dinnerBar/canyin/chushituan/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20201217172030.png"),
    starLImageUrl: "/dinnerBar/ic_list_star_red%402x.png",
    starLGrayImageUrl: "/dinnerBar/ic_list_star_grey_bg%402x.png",
    starNImageUrl: "/dinnerBar/canyin/canyin-xingji.png",
    packageStarImageUrl: "/dinnerBar/ic_list_star_red%402x.png",
    packageGrayStarImageUrl: "/dinnerBar/canyin/canyin-pingfen-gray.png",
    cookImageUrl: "/dinnerBar/canyin/chushituan/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20201217171938.png",
    packageImageUrl: "/dinnerBar/canyin/chushituan/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20201217171824.png",
    dishImageUrl: "/dinnerBar/canyin/chushituan/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20201217172059.png",
    shopId: '',
    shopForm: '',
    pageNum: 0,
    pageSize: 10,
    chefForm: [],
    productForm: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.data.shopId = options.id
    this.findClick()
    this.chefClick()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  findClick() {
    var _this = this
    find(_this.data.shopId).then(res => {
      console.log(res)
      _this.data.shopForm = res.data.shop
      _this.data.shopForm.logo = util.thumbnail(_this.data.shopForm.logo, 750, 750)
      _this.data.shopForm.score = 5 //TODO 默认五星
      _this.productListClick()
      _this.setData({
        shopForm: _this.data.shopForm
      })
    })
  },

  chefClick() {
    var _this = this
    list({
      shopId: this.data.shopId, pageNum: 1,
      pageSize: 1000,
    }).then(res => {
      console.log(res)
      res.data.data.forEach(function (item) {
        item.avatar = util.thumbnail(item.avatar, 120, 120)
      })
      _this.data.chefForm = res.data.data
      _this.setData({
        chefForm: _this.data.chefForm
      })
    })
  },

  productListClick() {
    var _this = this
    _this.data.pageNum += 1
    productList({
      mchId: _this.data.shopForm.mchId, pageSize: _this.data.pageSize,
      pageNum: _this.data.pageNum,
    }).then(res => {
      console.log(res)
      if (_this.data.pageNum == 1) {
        _this.data.productForm = []
      }
      res.data.data.forEach(item => {
        item.review = item.review.toFixed(1)
        item.thumbnail = util.thumbnail(item.thumbnail, 240, 180)
        item.review = '5.0' // TODO 默认五星
        _this.data.productForm.push(item)
      })
      _this.setData({
        productForm: _this.data.productForm,
        pageNum: _this.data.pageNum
      })
    })
  },

  //+++++++++++上拉加载+++++++++
  onReachBottom: function (callback) {
    console.log('我加载了')
    this.productListClick(() => {
      if (callback) {
        callback()
      }
    })

  },



  /**
   * 进入厨师详情页
   */
  toCookDetail: function (e) {
    console.info("e.currentTarget.dataset.cookId----" + e.currentTarget.dataset.cookId.id)
    console.info(e)
    wx.navigateTo({
      url: `../cookDetail/cookDetail?cookId=${e.currentTarget.dataset.cookId.id}`,
    })
  },

  /**
   * 进入套餐详情页面
   */
  toPackageDetail: function (e) {
    console.log(e)
    var _this = this
    // wx.navigateTo({
    //   url: `../packageDetail/packageDetail?packageId=${e.currentTarget.dataset.packageId}`,
    // })
    wx.navigateTo({
      url: '/pages/cook/packageDetail/packageDetail?id=' + e.currentTarget.dataset.item.id + '&shopId=' + _this.data.shopId,
    })
  }
})
